/**
 * 员工模糊搜索
 */
import React from 'react';
import { Select } from 'antd';
import { useRequest } from '@/hooks';
import { getEmployeeInfoList } from '@/services/components/userform';

const Employee = React.forwardRef(({ value = {}, onChange, defaultInitData, ...rest }, ref) => {
    const { data = {}, run } = useRequest(getEmployeeInfoList, {
        debounceInterval: 500,
        manual: true
    });
    // const { firstOrgInfo = {} } = user;
    // const { firstCompanyCode } = firstOrgInfo;

    const handleSearch = _value => {
        if (_value) {
            run({
                // administrativeOrganizationCode: firstCompanyCode,
                name: _value
            });
        }
    };

    const handleChange = (_value, item = {}) => {
        const { props = {} } = item;
        onChange({ value: props.value, label: props.children });
    };

    const { data: sourceData = [] } = data;
    const options = React.useMemo(() => {
        if (sourceData && sourceData.length > 0) {
            return sourceData.map(item => (
                <Select.Option key={item.employeeCode} value={item.employeeCode}>
                    {item.employeeName}
                </Select.Option>
            ));
        }
        return [];
    }, [sourceData]);

    React.useEffect(() => {
        if (defaultInitData) {
            run();
        }
    }, [defaultInitData]);

    return (
        <Select
            showSearch
            value={value.value}
            defaultActiveFirstOption={false}
            showArrow={false}
            filterOption={false}
            allowClear
            onSearch={handleSearch}
            onChange={handleChange}
            notFoundContent={null}
            placeholder="请选择"
            ref={ref}
            {...rest}
        >
            {options}
        </Select>
    );
});

export default Employee;
